package cn.iocoder.yudao.module.member.user.vo;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.api.infrastructure.social.enums.SocialTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

@Schema(description = "用户 APP - 手机 + 验证码登录 Request VO,如果登录并绑定社交用户，需要传递 social 开头的参数")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AppAuthSmsLoginReqVO {

    @Schema(description = "手机号", requiredMode = Schema.RequiredMode.REQUIRED, example = "15285116029")
    @NotEmpty(message = "手机号不能为空")
    private String mobile;

    @Schema(description = "短信验证码，开发环境/测试环境不真实的发送短信，默认验证码为“9999”", requiredMode = Schema.RequiredMode.REQUIRED, example = "9999") // FIXME 说明
    @NotEmpty(message = "短信验证码不能为空")
    @Length(min = 4, max = 6, message = "短信验证码长度为 4-6 位")
    @Pattern(regexp = "^[0-9]+$", message = "短信验证码必须都是数字")
    private String code;

    // ========== 绑定社交登录时，需要传递如下参数 ==========

    @Schema(description = "社交平台的类型，参见 SocialTypeEnum 枚举值：10-码云、20-钉钉、30-企业微信、31-微信公众平台、32-微信开放平台、34-微信小程序", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "") // FIXME 说明
    @InEnum(SocialTypeEnum.class)
    private Integer socialType;

    @Schema(description = "授权码", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "")
    private String socialCode;

    @Schema(description = "state 在 OAuth 的流程中的主要作用就是保证请求完整性，防止 CSRF 风险，可随机生成字符串，如 UUID", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "")
    private String socialState;

    @Schema(description = "邀请人引荐码", example = "82403003")
    private String inviteCode;

    @SuppressWarnings("unused")
    @AssertTrue(message = "授权码不能为空")
    public boolean isSocialCodeValid() {
        return socialType == null || StrUtil.isNotEmpty(socialCode);
    }

    @SuppressWarnings("unused")
    @AssertTrue(message = "授权 state 不能为空")
    public boolean isSocialState() {
        return socialType == null || StrUtil.isNotEmpty(socialState);
    }

}